package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.mapper.ClientMapper;
import cn.wolfcode.crm.query.ClientQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class ClientServiceImpl implements IClientService {

    @Autowired
    private ClientMapper clientMapper;

    //保存和更新客户操作
    public int saveOrUpdate(Client record) {
        if (record.getId() == null) {
            return clientMapper.insert(record);
        } else {
            return clientMapper.updateByPrimaryKey(record);
        }
    }

    //查询单个客户操作
    public Client selectByPrimaryKey(Long id) {
        return clientMapper.selectByPrimaryKey(id);
    }


    //客户列表操作
    public List<Client> selectAll() {
        return clientMapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        int count = clientMapper.queryCount(qo);
        if (count == 0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Client> data = clientMapper.queryList(qo);
        return new PageResult(count, data);
    }

    @Override
    public void delete(Long id) {
        clientMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void signing(Long id) {
        clientMapper.signing(id);
    }
}
